#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2025. Huawei Technologies Co.,Ltd. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import os
import tempfile
from pathlib import Path

from engine_server.utils.validators import Validator, StringValidator, MapValidator, DirectoryValidator, \
    RankSizeValidator, FileValidator, IntValidator, ClassValidator

BIN_PATH = "/usr/bin"
DIRECTORY_BLACKLIST_PATH = "/abc/d/e"

def test_validator_should_return_default_if_invalid():
    validation = Validator("aa")
    validation.register_checker(lambda x: len(x) < 5, "length of string should be less than 5")
    assert validation.is_valid()
    validation = Validator("123456")
    validation.register_checker(lambda x: len(x) < 5, "length of string should be less than 5")
    assert not validation.is_valid()
    assert validation.get_value("DEFAULT") == "DEFAULT"

def test_string_validator_max_len_parameter():
    assert not StringValidator("aa.1245", max_len=3).check_string_length().check().is_valid()
    assert StringValidator("aa.1245", max_len=30).check_string_length().check().is_valid()
    # default infinity
    assert StringValidator("aa.1234564646546").check_string_length().check().is_valid()

def test_string_validator_min_len_parameter():
    assert not StringValidator("aa", min_len=3).check_string_length().check().is_valid()
    assert StringValidator("aaa", min_len=3).check_string_length().check().is_valid()
    # default infinity
    assert StringValidator("a").check_string_length().check().is_valid()

def test_string_validator_can_be_transformed2int():
    assert not StringValidator("a").can_be_transformed2int().check().is_valid()
    assert not StringValidator("9" * 20).can_be_transformed2int().check().is_valid()
    assert not StringValidator("1,2").can_be_transformed2int().check().is_valid()
    assert StringValidator("12").can_be_transformed2int().check().is_valid()
    assert not StringValidator("12").can_be_transformed2int(min_value=100, max_value=200).check().is_valid()

def test_string_validator_contain_sensitive_words():
    assert not StringValidator("passwordme").check_not_contain_black_element("pass")\
                     .check_string_length().check().is_valid()

def test_map_validator_should_contain_inclusive_keys():
    map_validator = MapValidator({"a": True, "b": {"c": "1234"}}, inclusive_keys=["a", "b"])
    assert map_validator.is_valid()

def test_directory_black_list():
    assert not DirectoryValidator(DIRECTORY_BLACKLIST_PATH).with_blacklist(
        lst=[DIRECTORY_BLACKLIST_PATH]).check().is_valid()
    assert DirectoryValidator(DIRECTORY_BLACKLIST_PATH).with_blacklist(
        lst=[""]).check().is_valid()
    assert DirectoryValidator(DIRECTORY_BLACKLIST_PATH).with_blacklist(["/abc/d/"], exact_compare=True) \
                    .check().is_valid()
    # if not exact compare, the /abc/d/e is chirldren path of /abc/d/, so it is invalid
    assert not DirectoryValidator(DIRECTORY_BLACKLIST_PATH) \
                     .with_blacklist(["/abc/d/"], exact_compare=False).check().is_valid()
    assert DirectoryValidator("/usr/bin/bash").with_blacklist().check().is_valid()
    assert not DirectoryValidator("/usr/bin/bash").with_blacklist(exact_compare=False).check().is_valid()

def test_remove_prefix():
    assert DirectoryValidator.remove_prefix(BIN_PATH, None)[1] == BIN_PATH
    assert DirectoryValidator.remove_prefix(BIN_PATH, "")[1] == BIN_PATH
    assert DirectoryValidator.remove_prefix(None, "abc")[1] is None
    assert DirectoryValidator.remove_prefix("/usr/bin/python", BIN_PATH)[1] == "/python"

def test_directory_white_list():
    assert DirectoryValidator.check_is_children_path("/abc/d", DIRECTORY_BLACKLIST_PATH)
    assert DirectoryValidator.check_is_children_path("/abc/d", "/abc/d")
    assert not DirectoryValidator.check_is_children_path("/abc/d", "/abc/de")
    assert DirectoryValidator.check_is_children_path("/usr/bin", "/usr/bin/bash")

def test_directory_soft_link():
    tmp = tempfile.NamedTemporaryFile(delete=True)
    temp_dir = tempfile.mkdtemp()
    path = os.path.join(temp_dir, "link.ink")
    # make a soft link
    os.symlink(tmp.name, path)

    try:
        # do stuff with temp
        tmp.write(b"stuff")
        assert not DirectoryValidator(path).check_not_soft_link().check().is_valid()
    finally:
        tmp.close()
        os.remove(path)
        os.removedirs(temp_dir)

def test_directory_check():
    assert not DirectoryValidator("a/b/.././c/a.txt").check_is_not_none().check_dir_name().check().is_valid()
    assert not DirectoryValidator("").check_is_not_none().check_dir_name().check().is_valid()
    assert not DirectoryValidator(None).check_is_not_none().check_dir_name().check().is_valid()
    assert DirectoryValidator("a/bc/d").check_is_not_none().check_dir_name().check().is_valid()
    assert DirectoryValidator("/user/restore/fault/config", max_len=255). \
                    check_is_not_none().check_dir_name(). \
                    path_should_exist(is_file=True, msg="can not find the fault ranks config file") \
                    .should_not_contains_sensitive_words().with_blacklist().check()
    assert DirectoryValidator(os.path.dirname(__file__), max_len=255). \
                    check_is_not_none().check_dir_name().check_dir_file_number() \
                    .path_should_exist(is_file=False, msg="can not find the fault ranks config file") \
                    .should_not_contains_sensitive_words().with_blacklist().check()
    assert not DirectoryValidator(os.path.dirname(__file__)).path_should_not_exist().check().is_valid()

def test_check_directory_permissions():
    temp_dir = tempfile.TemporaryDirectory()
    temp_path = Path(temp_dir.name)
    test_dir = temp_path / "test_dir"
    test_dir.mkdir()
    os.chmod(test_dir, 0o777)
    target_mode = 0o750
    assert not DirectoryValidator(test_dir).check_directory_permissions(target_mode).check().is_valid()

def test_rank_size_check():
    assert not RankSizeValidator(4096).check_rank_size_valid().check().is_valid()
    assert not RankSizeValidator(0).check_device_num_valid().check().is_valid()
    assert RankSizeValidator(1).check_rank_size_valid().check().is_valid()

def test_file_check():
    file_path = os.path.join(os.path.dirname(__file__), "test_data", "test.txt")
    assert not FileValidator(file_path).check_file_size().check().is_valid()
    assert FileValidator(file_path).check_not_soft_link().check().is_valid()
    os.chown(file_path, os.getuid(), os.getgid())
    assert FileValidator(file_path).check_user_group().check().is_valid()

def test_int_check():
    assert IntValidator(1, min_value=0, max_value=12).check_value().check().is_valid()

def test_class_check():
    assert ClassValidator(2, int).check_isinstance().is_valid()
